<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
$id = isset($_POST["id"]) ? htmlspecialchars(trim($_POST["id"])) : "";
$BasePay = isset($_POST["BasePay"]) ? htmlspecialchars(trim($_POST["BasePay"])) : "";
$ratio = isset($_POST["ratio"]) ? htmlspecialchars(trim($_POST["ratio"])) : "";
$year = isset($_POST["year"]) ? htmlspecialchars(trim($_POST["year"])) : "";
$month = isset($_POST["month"]) ? htmlspecialchars(trim($_POST["month"])) : "";
if($id && $BasePay !== "" && $ratio !== ""){
	$db->sql = "UPDATE `PerformFactor` SET `BasePay`='{$BasePay}',`ratio`=$ratio WHERE id={$id}";
	$res = $db->doExecute();
	$msg = $res ? "修改成功" : "修改失败";
	echo "<script type='text/javascript'>alert('$msg');</script>";
}
else if($BasePay !== "" && $ratio !== "" && $year && $month){
	$db->sql = "SELECT * FROM `PerformFactor` WHERE time='{$year}-{$month}'";
	$data = $db->doResultList2();
	if($data){
		echo "<script type='text/javascript'>alert('{$year}年{$month}月的数据已设置过');</script>";
	}
	else{
		$db->sql = "INSERT INTO `PerformFactor`(`time`, `BasePay`, `ratio`) VALUES('{$year}-{$month}', '$BasePay', $ratio)";
		$res = $db->doExecute();
		$msg = $res ? "添加成功" : "添加失败";
		echo "<script type='text/javascript'>alert('$msg');</script>";
	}
}
$db->sql="SELECT * FROM `PerformFactor` ORDER BY `time` asc";
$factors = $db->doResultList2();
$num = count($factors);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<style type="text/css">
<!--
th,td{text-align: center;}
#messages form p,#messages2 form p{border-style: none; padding: 0;margin-top: 5px;text-align: left;}
#messages p,#messages2 p{text-align: left;}
-->
</style>
<script type="text/javascript">
$(function($){
	$(".editbutton").click(function(){
		var obj = $(this).parents("tr");
		var id = obj.find("td").eq(0).html();
		var BasePay = obj.find("td").eq(2).html();
		var ratio = obj.find("td").eq(3).html();
		$("#ID").val(id);
		$("#BasePay").val(BasePay);
		$("#ratio").val(ratio);
		$("#message_click").click();
	})
	
	$(".addbutton").click(function(){
		$("#message2_click").click();
	})

	$(".modify").live('click', function(){
		var i = 0;
		var j = 0;
		$(this).parents("form").find(".text-input").each(function(){													
			if($(this).val().replace(/(^\s*)|(\s*$)/g,"").length == 0){				
				return i = 1;
			}
			else if(isNaN($(this).val())){
				return j = 1;
			}
		})
		if(i){
			alert("有未填项！");
			return false;
		}
		else if(j){
			alert("请输入数字！");
			return false;
		}	
	})

	$(".add").live('click', function(){
		var i = 0;
		var j = 0;
		$(this).parents("form").find(".text-input").each(function(){													
			if($(this).val().replace(/(^\s*)|(\s*$)/g,"").length == 0){				
				return i = 1;
			}
			else if(isNaN($(this).val())){
				return j = 1;
			}
		})
		if(i){
			alert("有未填项！");
			return false;
		}
		else if(j){
			alert("请输入数字！");
			return false;
		}	
	})
})

</script>

<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>绩效参数修改</strong>
			</p>
			<form action="" method="post">
				<fieldset>
					<p>					
						底薪：
						<input class="text-input" type="text" style="margin-left: 35px;" id="BasePay" name="BasePay" />
					</p>
					<p>					
						总奖励因子：
						<input class="text-input" type="text" id="ratio" name="ratio" />
					</p>
					<p>	
						<input id="ID" type="hidden" value="" name="id"/>							
						<input class="button modify" type="submit" value="修改" /> 
						<a href="javascript:void(0);" style="display: inline-block;margin-left: 10px;" onclick="$(this).parents('#facebox').hide();">关闭</a>
					</p>
				</fieldset>
			</form>
		</div>
		<a href="#messages2" rel="modal" id="message2_click"></a>
		<div id="messages2" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>日期绩效参数新增</strong>
			</p>
			<form action="" method="post">
				<fieldset>
					<p>					
						底薪：
						<input class="text-input" type="text" style="margin-left: 35px;" name="BasePay" />
					</p>
					<p>					
						总奖励因子：
						<input class="text-input" type="text" name="ratio" />
					</p>
					<p>
						日期：
						<select name="year" style="margin-left: 35px;">
						<?php for($i=2013;$i<=date("Y");$i++):?>
							<option value="<?php echo $i;?>"><?php echo $i;?></option>
						<?php endfor;?>
						</select>年
						<select name="month">
						<?php for($i=1;$i<=12;$i++):?>
							<option value="<?php echo ($i<10) ? "0$i" : $i;?>"><?php echo ($i<10) ? "0$i" : $i;?></option>
						<?php endfor;?>
						</select>月
					</p>
					<p>														
						<input class="button add" type="submit" value="增加" /> 
						<a href="javascript:void(0);" style="display: inline-block;margin-left: 10px;" onclick="$(this).parents('#facebox').hide();">关闭</a>
					</p>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">接下来你要做什么呢亲？</p>
			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3>绩效参数列表(<span style="font-size: 14px;color: red;">默认底薪：1800，默认总奖励因子：1</span>)</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>							
							<thead>
								<tr>
									<th>编号</th>
									<th>年月</th>									
									<th>底薪</th>
									<th>总奖励因子</th>									
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
							<?php if($num > 0):?>
								<?php foreach($factors as $key => $val):?>
								<tr>
									<td><?php echo $val['id'];?></td>
									<td><?php echo $val['time'];?></td>
									<td><?php echo $val['BasePay'];?></td>									
									<td><?php echo $val['ratio'];?></td>																		
									<td>
										<!-- Icons --> 
										<a  title="Edit" class="editbutton" style="cursor: pointer;display: inline-block;width: 30px;">修改</a> 
										<?php if($key == $num-1):?>
										<a  title="Edit" class="addbutton" style="cursor: pointer;display: inline-block;width: 30px;">新增</a> 
										<?php endif;?>										
										<!-- <a href="#" title="Edit Meta"><img src="resources/images/icons/hammer_screwdriver.png" alt="Edit Meta" /></a> -->
									</td>
								</tr>
								<?php endforeach;?>
							<?php endif;?>
							</tbody>
						</table>
					</div>
					<!-- End #tab1 -->
				</div>
				<!-- End .content-box-content -->
			</div>
			<!-- End .content-box -->
			<div class="clear"></div>
			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->
		</div>
		<!-- End #main-content -->
	</div>
</body>
<!-- Download From www.exet.tk-->
</html>
